﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Tomahawk.Runtime.Timeline
{
    public class TextureOffsetEffectTrack : SpriteEffectTrack
    {
        [XmlElement("OffsetX")]
        public string offsetX = "";

        [XmlElement("OffsetY")]
        public string offsetY = "";

        [XmlElement("DefaultOffsetX")]
        public float defaultOffsetX = 0.0f;

        [XmlElement("DefaultOffsetY")]
        public float defaultOffsetY = 0.0f;

        private Vector2 offset = Vector2.Zero;
        private Curve curveOffsetX = new Curve();
        private Curve curveOffsetY = new Curve();

        public TextureOffsetEffectTrack() : base() { }

        public TextureOffsetEffectTrack(string name) : base(name) { }

        protected override string GetEffectFile()
        {
            return @"Tomahawk.Content\SpriteEffects\TextureOffsetEffect";
        }

        protected override void OnLoadEffectParameters(Effect effect)
        {
            effect.Parameters["Offset"].SetValue(offset);
        }

        protected override bool OnLoad(GraphicsDevice graphics, ContentManager contentManager)
        {
            curveOffsetX = LoadCurve(contentManager, offsetX);
            curveOffsetY = LoadCurve(contentManager, offsetY);
            return base.OnLoad(graphics, contentManager);
        }

        public override void SortKeys() { }

        public override bool Update(float time)
        {
            offset.X = curveOffsetX != null ? curveOffsetX.Evaluate(time) : defaultOffsetX;
            offset.Y = curveOffsetY != null ? curveOffsetY.Evaluate(time) : defaultOffsetY;

            return true;
        }
    }
}
